import 'package:flutter/material.dart';
import 'package:flutter_douban/config/app_colors.dart';
import 'package:flutter_douban/main_page/home/activity/home_activity_page.dart';
import 'package:flutter_douban/main_page/home/anchorbaking/anchor_baking_page.dart';
import 'package:flutter_douban/main_page/home/attention/home_image_page.dart';
import 'package:flutter_douban/main_page/home/mother/home_mother_page.dart';
import 'package:flutter_douban/main_page/home/notes/home_notes_page.dart';
import 'package:flutter_douban/main_page/home/recommend/home_recomend_page.dart';
import 'package:flutter_douban/main_page/home/video/home_video_page.dart';
import 'package:flutter_douban/tool_views/cached_network_Image.dart';

import 'my_underline_tabIndicator.dart';

const List<Tab> _tabs = [
  Tab(text: '  菜谱  '),
  Tab(text: '  笔记  '),
  Tab(text: '  收藏  '),
];

final List<Widget> _tabaContent = [
  HomeAttentionPage(),
  HomeRecomendPage(),
  HomeNoteGridPage(),
];

class XuanPage extends StatefulWidget {
  const XuanPage({Key? key}) : super(key: key);

  @override
  _XuanPageState createState() => _XuanPageState();
}

class _XuanPageState extends State<XuanPage> with TickerProviderStateMixin {
  late TabController _tabController;

  /// 初始化方法 请求数据
  @override
  void initState() {
    super.initState();

    _tabController = TabController(
      vsync: this,
      length: _tabs.length,
      initialIndex: 1, // 默认选中哪一个
    );

    _tabController.addListener(() {
      _tabController.index;
      print('切换了item');
    });
  }

  @override
  void dispose() {
    // 释放对象
    _tabController.dispose();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Container(
        child: NestedScrollView(
          headerSliverBuilder: (BuildContext context, bool innerBoxIsScrolled) {
            return <Widget>[
              SliverAppBar(
                expandedHeight: 230.0,
                pinned: true,
                flexibleSpace: Padding(
                  padding: EdgeInsets.symmetric(vertical: 8),
                  child: PageView(),
                ),
              ),
              SliverPersistentHeader(
                pinned: true,
                delegate: StickyTabBarDelegate(
                  child: TabBar(
                    // isScrollable: true,
                    unselectedLabelStyle: TextStyle(
                      fontSize: 16,
                    ),
                    labelStyle:
                        TextStyle(fontSize: 16, fontWeight: FontWeight.bold),
                    indicator: MyUnderlineTabIndicator(
                        borderSide:
                            BorderSide(width: 3.0, color: AppColors.yellow)),
                    controller: this._tabController,
                    tabs: _tabs,
                  ),
                ),
              ),
            ];
          },
          body: TabBarView(
            controller: this._tabController,
            children: _tabaContent,
          ),
        ),
      ),
    );
  }
}

class StickyTabBarDelegate extends SliverPersistentHeaderDelegate {
  final TabBar child;

  StickyTabBarDelegate({required this.child});

  @override
  Widget build(
      BuildContext context, double shrinkOffset, bool overlapsContent) {
    return Container(
      color: Colors.white,
      child: this.child,
    );
  }

  @override
  double get maxExtent => this.child.preferredSize.height;

  @override
  double get minExtent => this.child.preferredSize.height;

  @override
  bool shouldRebuild(SliverPersistentHeaderDelegate oldDelegate) {
    return true;
  }
}
